package org.coonect;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.GroupLayout;
import javax.swing.JOptionPane;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JTextArea;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Color;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.border.LineBorder;

public class First {

	private JFrame frame;
	private JTextField textField;
	private JTextField textField_1;
	private JTextField textField_2;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					First window = new First();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public First() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	public void initialize() {
		frame = new JFrame();
		frame.getContentPane().setBackground(Color.CYAN);
		frame.setResizable(false);
		frame.setTitle("Унос података");
		frame.setBounds(100, 100, 679, 689);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		JLabel label = new JLabel("Унесите текст наслова");

		textField = new JTextField();
		textField.setBorder(new LineBorder(new Color(0, 0, 0)));
		textField.setColumns(10);

		JLabel label_1 = new JLabel("Унесите текст огласа");

		final JTextArea textField1 = new JTextArea();
		textField1.setBorder(new LineBorder(new Color(0, 0, 0)));
		textField1.setLineWrap(true);

		JLabel label_2 = new JLabel("Унесите датум објаве огласа");

		JButton button = new JButton("Сними промене у базу");

		button.addActionListener(new ActionListener() {
			// private JTextArea textArea3;
			// private JTextArea textArea2;
			public void actionPerformed(ActionEvent arg0) {
				// Savedata s= new Savedata();
				String naslov = textField.getText().toString();
				String tekst = textField1.getText().toString();

				String du = textField_1.getText().toString();
				String di = textField_2.getText().toString();
				if (naslov.length() > 1 && tekst.length() > 1) {
					save(naslov, tekst, du, di);

				}

			}
		});

		JButton button_1 = new JButton("Одустани");

		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {

				Choose.main(null);
				frame.setVisible(false);
			}
		});

		JLabel label_3 = new JLabel("Унесите датум истека огласа");

		textField_1 = new JTextField();
		textField_1.setColumns(10);

		textField_2 = new JTextField();
		textField_2.setColumns(10);
		GroupLayout groupLayout = new GroupLayout(frame.getContentPane());
		groupLayout
				.setHorizontalGroup(groupLayout
						.createParallelGroup(Alignment.TRAILING)
						.addGroup(
								Alignment.LEADING,
								groupLayout
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												groupLayout
														.createParallelGroup(
																Alignment.LEADING)
														.addGroup(
																groupLayout
																		.createSequentialGroup()
																		.addComponent(
																				textField_1,
																				GroupLayout.PREFERRED_SIZE,
																				148,
																				GroupLayout.PREFERRED_SIZE)
																		.addContainerGap())
														.addGroup(
																groupLayout
																		.createSequentialGroup()
																		.addGroup(
																				groupLayout
																						.createParallelGroup(
																								Alignment.TRAILING)
																						.addComponent(
																								textField1,
																								Alignment.LEADING,
																								GroupLayout.DEFAULT_SIZE,
																								601,
																								Short.MAX_VALUE)
																						.addGroup(
																								groupLayout
																										.createSequentialGroup()
																										.addGroup(
																												groupLayout
																														.createParallelGroup(
																																Alignment.TRAILING)
																														.addGroup(
																																groupLayout
																																		.createSequentialGroup()
																																		.addGroup(
																																				groupLayout
																																						.createParallelGroup(
																																								Alignment.LEADING)
																																						.addComponent(
																																								label,
																																								GroupLayout.PREFERRED_SIZE,
																																								192,
																																								GroupLayout.PREFERRED_SIZE)
																																						.addComponent(
																																								label_1,
																																								GroupLayout.PREFERRED_SIZE,
																																								166,
																																								GroupLayout.PREFERRED_SIZE))
																																		.addPreferredGap(
																																				ComponentPlacement.RELATED,
																																				18,
																																				Short.MAX_VALUE))
																														.addComponent(
																																button_1))
																										.addGroup(
																												groupLayout
																														.createParallelGroup(
																																Alignment.LEADING,
																																false)
																														.addComponent(
																																textField,
																																GroupLayout.DEFAULT_SIZE,
																																391,
																																Short.MAX_VALUE)
																														.addComponent(
																																button,
																																Alignment.TRAILING)))
																						.addGroup(
																								groupLayout
																										.createSequentialGroup()
																										.addComponent(
																												label_2)
																										.addPreferredGap(
																												ComponentPlacement.RELATED,
																												138,
																												Short.MAX_VALUE)
																										.addGroup(
																												groupLayout
																														.createParallelGroup(
																																Alignment.LEADING)
																														.addComponent(
																																textField_2,
																																GroupLayout.PREFERRED_SIZE,
																																147,
																																GroupLayout.PREFERRED_SIZE)
																														.addGroup(
																																Alignment.TRAILING,
																																groupLayout
																																		.createSequentialGroup()
																																		.addComponent(
																																				label_3)
																																		.addGap(
																																				44)))))
																		.addGap(
																				56)))));
		groupLayout.setVerticalGroup(groupLayout.createParallelGroup(
				Alignment.LEADING).addGroup(
				groupLayout.createSequentialGroup().addContainerGap().addGroup(
						groupLayout.createParallelGroup(Alignment.BASELINE)
								.addComponent(label, GroupLayout.DEFAULT_SIZE,
										42, Short.MAX_VALUE).addComponent(
										textField, GroupLayout.PREFERRED_SIZE,
										GroupLayout.DEFAULT_SIZE,
										GroupLayout.PREFERRED_SIZE))
						.addPreferredGap(ComponentPlacement.UNRELATED)
						.addComponent(label_1, GroupLayout.PREFERRED_SIZE, 40,
								GroupLayout.PREFERRED_SIZE).addPreferredGap(
								ComponentPlacement.RELATED).addComponent(
								textField1, GroupLayout.PREFERRED_SIZE, 297,
								GroupLayout.PREFERRED_SIZE).addPreferredGap(
								ComponentPlacement.UNRELATED).addGroup(
								groupLayout.createParallelGroup(
										Alignment.BASELINE).addComponent(
										label_2).addComponent(label_3)).addGap(
								18).addGroup(
								groupLayout.createParallelGroup(
										Alignment.BASELINE).addComponent(
										textField_1,
										GroupLayout.PREFERRED_SIZE,
										GroupLayout.DEFAULT_SIZE,
										GroupLayout.PREFERRED_SIZE)
										.addComponent(textField_2,
												GroupLayout.PREFERRED_SIZE,
												GroupLayout.DEFAULT_SIZE,
												GroupLayout.PREFERRED_SIZE))
						.addGap(95).addGroup(
								groupLayout.createParallelGroup(
										Alignment.BASELINE)
										.addComponent(button).addComponent(
												button_1)).addGap(77)));
		frame.getContentPane().setLayout(groupLayout);
	}

	public void save(String naslov, String tekst, String du, String di) {

		Connection con = null;
		PreparedStatement pst = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		String slika = "http://prvisud.rs/slike/user1.png";
		long timestamp = System.currentTimeMillis() / 1000;
		Properties props = new Properties();
		FileInputStream in = null;

		try {
			in = new FileInputStream("database.properties");
			props.load(in);

		} catch (FileNotFoundException ex) {

			Logger lgr = Logger.getLogger(ConnectionToBase.class.getName());
			lgr.log(Level.SEVERE, ex.getMessage(), ex);

		} catch (IOException ex) {

			Logger lgr = Logger.getLogger(ConnectionToBase.class.getName());
			lgr.log(Level.SEVERE, ex.getMessage(), ex);

		} finally {

			try {
				if (in != null) {
					in.close();
				}
			} catch (IOException ex) {
				Logger lgr = Logger.getLogger(ConnectionToBase.class.getName());
				lgr.log(Level.SEVERE, ex.getMessage(), ex);
			}
		}

		// String url = props.getProperty("db.url");
		String url = "jdbc:h2:~/test?useEncoding=true&characterEncoding=UTF-8";
		String user = "sa";
		String passwd = "";

		try {

			con = DriverManager.getConnection(url, user, passwd);

			try {

				// Prepare a statement to insert a record

				stmt = con
						.prepareStatement("INSERT INTO DATABASE1 (NASLOV,TEKST,DATUM_POCETKA,DATUM_KRAJA,SLIKA,COUNTER) VALUES(?,?,?,?,?,?)");
				stmt.setString(1, naslov);
				stmt.setString(2, tekst);
				stmt.setString(3, du);
				stmt.setString(4, di);
				stmt.setString(5, slika);
				stmt.setLong(6, timestamp);
				// Execute the insert statement
				stmt.executeUpdate();
			} catch (SQLException e) {
				System.out.print(e.toString());
			}

		} catch (Exception ex) {
			Logger lgr = Logger.getLogger(ConnectionToBase.class.getName());
			lgr.log(Level.SEVERE, ex.getMessage(), ex);

		} finally {

			try {
				if (rs != null) {
					rs.close();
				}
				if (pst != null) {
					pst.close();
				}
				if (con != null) {
					con.close();
				}

			} catch (SQLException ex) {
				Logger lgr = Logger.getLogger(ConnectionToBase.class.getName());
				lgr.log(Level.WARNING, ex.getMessage(), ex);
			}
		}
		JOptionPane.showMessageDialog(frame,
				"Успешно сте снимили промене у базу података", "Порука",
				JOptionPane.PLAIN_MESSAGE);
		frame.setVisible(false);

	}

}
